! function() {
    var soundopen = false;
    creatSound('src/mp3/music.mp3');

    function creatSound(value) {
        $('audio').remove();
        if (value) {
            var audio = '<audio id="audioId1" src="' + value + '"  preload="preload" style="position: absolute; left:0; top: 0;">您的浏览器不支持 audio 标签。</audio>';
            $('body').append(audio);
            var sound = new soundMark();
            sound.init('audioId1', function() {
                sound.stop();
                sound.play();
                controlMusicIcon(false);
            });
            sound.play();
            $('#music').show();
            $('#music').on('touchstart', function() {
                if (!soundopen) {
                    sound.pause();
                    soundopen = true;
                } else {
                    sound.play();
                    soundopen = false;
                }
                controlMusicIcon(soundopen);
            })
        }
    }

    function controlMusicIcon(value) {
        if (value) {
            $('#music .open').hide();
            $('#music .close').show();
        } else {
            $('#music .open').show();
            $('#music .close').hide();
        }
    }

    function soundMark() {
        var me = this;
        this.doc = null;
        this.states = null;
        this.duration = null;

        //id html当中的id标签    //fun 声音结束函数调用   //load 音频文件已经可以播放了
        this.init = function(id, fun, load) {
            me.doc = document.getElementById(id)
            /* sound.doc.addEventListener('loadstart',function(){
            alert(sound.doc.duration + ','+ sound.doc.currentTime+' 1')   //1
        })

        sound.doc.addEventListener('canplay',function(){
            alert(sound.doc.duration+',' +sound.doc.currentTime+' 2')   //4
        })
        sound.doc.addEventListener('durationchange',function(){
            alert(sound.doc.duration+',' +sound.doc.currentTime+' 3')   //2
        })
        sound.doc.addEventListener('loadedmetadata',function(){
            alert(sound.doc.duration+',' +sound.doc.currentTime+' 4')   //3
        })*/
            if (load) {
                //console.log(0)
                //sound.doc.addEventListener('progress',function(){
                //alert('aaaa')
                load();
                //})
            }
            if (fun) {
                me.doc.addEventListener('ended', function() {
                    fun()
                });
            }
        };
        this.play = function() {
            me.doc.play()
            //this.addevent()
        };
        this.pause = function() {
            me.doc.pause()
        }
        this.stop = function() {
            me.doc.pause()
            me.doc.currentTime = 0
        }
        this.setCurrent = function(value) {
            me.doc.currentTime = value
        }
        this.getRate = function() {
            return me.doc.currentTime / me.doc.duration
        }
        this.current = function() {
            var cu = me.doc.currentTime
            return getHMS(cu)
        }
        this.length = function() {
            if (me.doc.duration) {
                var cu = me.doc.duration
                return getHMS(cu)
            } else {
                return null
            }
        }
        this.clone = function() {
            var tt = {}
            for (var i in sound) {
                tt[i] = sound[i]
            }
            return tt
        }
        this.addevent = function() {
            me.doc.addEventListener('canplay', function() {
                // sound.duration=this.duration
                alert(me.duration + ',' + me.currentTime + ' 2') //4
            })

            this.doc.addEventListener('loadedmetadata', function() {
                // sound.duration=sound.doc.duration
                alert(me.duration + ',' + me.currentTime + ' 4') //3
            })
        }

        function getHMS(value) {
            var h = Math.floor(value / 3600)
            var m = Math.floor((value - h * 3600) / 60)
            var s = Math.floor(value - h * 3600 - m * 60)
            if (h == 0) {
                return ('0' + m).slice(-2) + ':' + ('0' + s).slice(-2)
            } else {
                return ('0' + h).slice(-2) + ':' + ('0' + m).slice(-2) + ':' + ('0' + s).slice(-2)
            }
        }
        // Localize or create a new JavaScript Template object.
    };
}();